//
//  NewsViewController.m
//  iStore
//

#import "NewsViewController.h"
#import "APService.h"
#import "AppDelegate.h"
#import "MyStoreViewController.h"
#import "RequestEngine.h"
#import "messageModel.h"
#import "NewsTableViewCell.h"

@interface NewsViewController () <UITableViewDelegate,UITableViewDataSource>


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation NewsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息中心";
    
    _dataArray = [NSMutableArray array];
    
    //   NSLog(@"%@",_dataArray);
    NSString *userID = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];
    NSDictionary *dict = @{@"userID":userID};
    _tableView.delegate = self;
    
    [self activityStart];
    [RequestEngine RequestEngineWithDic:dict URL:@"message/getMSG" completed:^(NSString *errorcode, NSDictionary *resultDict) {
        if ([errorcode isEqualToString:@"0"]) {
            for (id obj in resultDict) {
                [_dataArray addObject:obj];
        
            }
            [self updataMessage];
            [_tableView reloadData];
        }
        [self activityStop];
        if (_dataArray.count == 0 || _dataArray == nil) {
            [self createNull];
        }
    }];
    
//    NSString *str =[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"content%@",userID]];
    
    
    
 //   NSLog(@"%@",_dataArray);
}
- (void)updataMessage
{
    for (id obj in _dataArray) {
        if ([[NSString stringWithFormat:@"%@",obj[@"isRead"]] isEqualToString:@"1"]) {
            [RequestEngine RequestEngineWithDic:@{@"id":[NSString stringWithFormat:@"%@",obj[@"id"]]} URL:@"message/updateMSG" completed:^(NSString *errorcode, NSDictionary *resultDict) {
                
            }];
        }
    }
}

- (void)createNull
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 40)];
    label.text = @"您还没有消息喔~";
    label.textColor = [UIColor blackColor];
    [label setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:label];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(goBacksActions)];


    
}

- (void)goBacksActions
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark -- tableView 协议---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellID = @"NewsTableViewCell";
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"NewsTableViewCell" owner:self options:nil]firstObject];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
    }
    [cell refreshData:_dataArray[indexPath.section]];
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    alert([_dataArray[indexPath.section] objectForKey:@"msgContent"]);
}
@end
